<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>课程设置</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>课程设置</legend>
        </fieldset>
        <br>
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">课程查询</label>
                    <div class="layui-input-inline">
                        <input type="text" name="key" placeholder="输入课程名称/所属专业" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn pear-btn-md" lay-submit
                            lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                </div>
            </div>
        </form>
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </div>
</script>

<script type="text/html" id="user-bar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </div>
</script>

<script src="../../../static/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let MODULE_PATH = "operate/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '课程ID',
                    field: 'courseId',
                    width: 100,
                    sort: true,
                },
                {
                    title: '课程名称',
                    field: 'courseName',
                    align: 'center'
                },
                {
                    title: '课程学分',
                    field: 'courseScore',
                    align: 'center',
                },
                {
                    title: '课程学时',
                    field: 'courseTime',
                    align: 'center',
                },
                {
                    title: '所属专业',
                    field: 'courseMajor',
                    align: 'center',
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]


        let tableIns = table.render({
            elem: '#user-table',
            url: '/course/getAllCourse',
            method: "post",
            title: '智慧校园系统课程信息表',
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip']
                , curr: 1
                , groups: 3
                , last: false
            },
            limit: 5,
            //limits:[5,10,20],
            cols: cols,
            id: 'user-table',
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新职位列表',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            //一定要加这个参数代码，否则会由于layui自身的bug导致无法分页
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                let result;
                // console.log(res.data[0].birthday);
                // res.data[0].birthday = dateFormat(res.data[0].birthday, 'Y-M-D')
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });


        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'edit') {
                //obj.del();
                window.edit(obj);
            } else if (obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function (data) {
            tableIns.reload({
                url: '/course/searchCourse',
                method: 'post',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip']
                    , curr: 1
                    , groups: 3
                    , last: false
                }
                , where: data.field
            })
            return false;
        });


        window.add = function () {
            layer.open({
                type: 2,
                title: '新增课程信息',
                shade: 0.1,
                shadeClose: true,
                area: ['40%', '90%'],
                content: MODULE_PATH + 'addCourse_user.html'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改课程信息：' + obj.data['courseName'],
                shade: 0.1,
                shadeClose: true,
                area: ['40%', '90%'],
                content: MODULE_PATH + 'editCourse.html',
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index);
                    //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    //console.log(obj.data);
                    let iframeWin = window[layero.find('iframe')[0]['name']]
                    body.find('#courseId').val(obj.data['courseId'])
                    body.find('#courseName').val(obj.data['courseName'])
                    body.find('#courseScore').val(obj.data['courseScore'])
                    body.find('#courseTime').val(obj.data['courseTime'])
                    body.find('#courseTime').val(obj.data['courseTime'])
                    body.find('#courseMajor').find("option[value='" +
                        obj.data['courseMajor'] + "']").prop("selected", true);
                    iframeWin.layui.form.render('select');
                }
            });
        }

        window.remove = function (obj) {
            console.log(obj.data['courseId']);
            layer.confirm('确定要删除该课程信息', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/course/deleteCourse",
                    data: {
                        courseId: obj.data['courseId']
                    },
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        console.log(result);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                tableIns.reload({
                                    url: "/course/getAllCourse",
                                    page: {
                                        layout: ['count', 'prev', 'page', 'next', 'skip']
                                        , curr: $(".layui-laypage-em").next().html()
                                        , groups: 3
                                        , last: false
                                    },
                                    method: "post"
                                });
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
        window.batchRemove = function (obj) {
            let checkIds = common.checkField(obj, 'courseId');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            //批量删除
            layer.confirm('确定要删除这些课程', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let checkStatus = table.checkStatus('user-table');
                let courseIds = [];
                for (let i = 0; i < checkStatus.data.length; i++) {
                    courseIds[i] = checkStatus.data[i].courseId;
                    //console.log(checkStatus.data[i].userId);
                }
                let loading = layer.load(2, {
                    shade: [0.2, '#323232'] //0.1透明度的白色背景
                });
                table.reload('user-table');
                $.ajax({
                    url: "/course/batchRemoveCourse",
                    data:{
                        courseIds:JSON.stringify(courseIds)
                    },
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                tableIns.reload({
                                    url: "/course/getAllCourse",
                                    page: {
                                        layout: ['count', 'prev', 'page', 'next', 'skip']
                                        , curr: 1
                                        , groups: 3
                                        , last: false
                                    },
                                    method: "post"
                                });
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('user-table', {
                url: "/course/getAllCourse",
                method: "post"
            });
        }
    })
</script>
</body>

</html>